package com.xyh.transaction.controller;

import com.github.pagehelper.PageInfo;
import com.xyh.transaction.entity.dto.order.CreateOrderDto;
import com.xyh.transaction.entity.dto.order.SearchOrderDto;
import com.xyh.transaction.entity.dto.user.ArrDto;
import com.xyh.transaction.entity.vo.LabelValue;
import com.xyh.transaction.entity.vo.OrderCheckoutInfo;
import com.xyh.transaction.entity.vo.OrderStatusVo;
import com.xyh.transaction.entity.vo.OrderVo;
import com.xyh.transaction.response.ResponseResult;
import com.xyh.transaction.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单管理")
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "订单信息", notes = "返回id的订单信息")
    @GetMapping("/{id}")
    public ResponseResult<OrderVo> orderInfo(@PathVariable String id) {
        OrderVo orderInfo = orderService.getOrderInfo(id);
        return ResponseResult.success("查询成功！", orderInfo);
    }

    @ApiOperation(value = "结算接口", notes = "返回当前用户购物车结算信息")
    @GetMapping("checkout")
    public ResponseResult<OrderCheckoutInfo> checkout(@RequestParam(required = false) Integer orderId) {
        OrderCheckoutInfo checkoutInfo = orderService.getCheckoutInfo(orderId);
        return ResponseResult.success("查询成功", checkoutInfo);
    }

    @ApiOperation(value = "订单状态接口",notes = "返回所有订单状态")
    @GetMapping("status")
    public ResponseResult<List<OrderStatusVo>> statusList(){
        List<OrderStatusVo> orderStatusList = orderService.getOrderStatusList();
        return ResponseResult.success("查询成功！",orderStatusList);
    }

    @ApiOperation(value = "支付方式接口",notes = "返回所有的支付方式枚举")
    @GetMapping("pay_type")
    public ResponseResult<List<LabelValue>> payTypeList(){
        List<LabelValue> payTypeList = orderService.getPayTypeList();
        return ResponseResult.success("查询成功！",payTypeList);
    }

    @ApiOperation(value = "送货方式接口",notes = "返回所有送货方式枚举")
    @GetMapping("delivery_type")
    public ResponseResult<List<LabelValue>> deliveryTypeList(){
        List<LabelValue> deliveryTypeList = orderService.getDeliveryTypeList();
        return ResponseResult.success("查询成功！",deliveryTypeList);
    }

    @ApiOperation(value = "创建订单", notes = "生成订单,返回订单id")
    @PostMapping("create")
    public ResponseResult<Integer> create(@RequestBody CreateOrderDto orderDto) {
        Integer orderId = orderService.createOrder(orderDto);
        return ResponseResult.success("操作成功！", orderId);
    }

    @ApiOperation(value = "订单数据", notes = "返回用户端个人中心订单数据")
    @GetMapping
    public ResponseResult<PageInfo<OrderVo>> userList(SearchOrderDto orderDto) {
        PageInfo<OrderVo> pageInfo = orderService.getUserOrderList(orderDto);

        return ResponseResult.success("查询成功！", pageInfo);
    }

    @ApiOperation(value = "订单列表",notes = "返回管理端订单数据列表")
    @PostMapping("list")
    public ResponseResult<PageInfo<OrderVo>> adminList(@RequestBody SearchOrderDto orderDto){
        PageInfo<OrderVo> pageInfo = orderService.getAminOrderList(orderDto);
        return ResponseResult.success("查询成功！",pageInfo);
    }

    @ApiOperation(value = "更新订单状态", notes = "根据id更新订单状态")
    @PutMapping("/status")
    public ResponseResult<Void> updateStatus(@RequestBody SearchOrderDto orderDto) {
        orderService.updateStatus(orderDto);
        return ResponseResult.success("操作成功！", null);
    }

    @ApiOperation(value = "发货接口",notes = "根据订单id修改订单状态")
    @PutMapping("/delivery")
    public ResponseResult<Void> deliveryOrder(@RequestBody ArrDto arrDto){
        orderService.deliveryOrder(arrDto);
        return ResponseResult.success("操作成功！",null);
    }

    @ApiOperation(value = "导出订单",notes = "导出订单数据xlsx")
    @PostMapping("/export")
    public ResponseEntity<byte[]> export(@RequestBody SearchOrderDto orderDto){
        return orderService.exportOrderList(orderDto);
    }

}
